﻿namespace EasyStep.TestActions.WebAii.Actions.Functions
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using EasyStep.Attributes;
    using EasyStep.TestExecution.Interfaces;
    using EasyStep.TestExecution.Data;
    using ArtOfTest.WebAii.Core;

    [TestAction(typeof(ExecuteBase), "Test", RequireLocator=true,RequireData=false,RequireScreenShot=true)]
    public class ExecuteTest : ITestAction<Manager>
    {
        public void Execute(Manager browser, TestStepResult data, IExecuteDataContext context)
        {
            data.ExecutionLog.AppendLine("Try to find test by name:" + data.Locator);
            var testInfo = context.TestInfoManager.GetTest(context.CurrentProject,data.Locator, true);
            data.ExecutionLog.AppendFormat("test been found?{0},\r\n contains:{1} test steps\r\n", testInfo != null, testInfo == null || testInfo.Steps == null ? -1 : testInfo.Steps.Count());
            if (testInfo == null)
            {
                data.Result = TestExecution.ExecutionResult.Failed;
                throw new NullReferenceException(string.Format("Can't Execute child Test (with name:[{0}]) for current project:[{1}]!", data.Locator, context.CurrentProject));
            }
            data.Children = testInfo.Steps.Select(s => new TestStepResult(s));
        }



        [TestAction(null, "Execute", IsAbstract = true)]
        public class ExecuteBase : ITestAction<Manager>
        {

            public void Execute(Manager browser, TestStepResult data, IExecuteDataContext context)
            {
                throw new NotImplementedException();
            }
        }
    }
}